package com.ymt.bpm.web.rest.engine.bpd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PBpd;
import com.ymt.bpm.model.PBpdWithBLOBs;
import com.ymt.bpm.service.engine.bpd.BpdAuthService;
import com.ymt.bpm.service.engine.bpd.BpdService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/3/23.
 */
@RestController
@RequestMapping("/api/v1/engine")
public class BpdResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BpdService bs;

    @Autowired
    private BpdAuthService bas;

    @RequestMapping(value="/bpd/query", method= RequestMethod.GET)
    public JSONObject query(HttpServletRequest req,
                                  @RequestParam(name="category", required = false)String category,
                                  @RequestParam(name="status", required = false)String status,
                                  @RequestParam(name="search", required = false)String search,
                                  @RequestParam(name="sort", required = false)String sort,
                                  @RequestParam(name="order", required = false)String order,
                                  @RequestParam("offset")int offset,
                                  @RequestParam("limit")int limit,
                                  @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("/bpd/query tenantId=" + tenantId);
            log.debug("/bpd/query category=" + category);
            log.debug("/bpd/query status=" + status);
            log.debug("/bpd/query search=" + search);
            log.debug("/bpd/query sort=" + sort);
            log.debug("/bpd/query order=" + order);
            log.debug("/bpd/query offset=" + offset);
            log.debug("/bpd/query limit=" + limit);
            log.debug("/bpd/query timezoneOffset=" + timezoneOffset);
        }
        return bs.query(tenantId, category, status, search, sort, order, offset, limit, lang, timezoneOffset);
    }

    @RequestMapping(value="/bpd/{bpdId}", method= RequestMethod.GET)
    public JSONObject get(HttpServletRequest req,
                                @PathVariable("bpdId")String bpdId,
                                @RequestParam(value="timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("get tenantId=" + tenantId);
            log.debug("get bpdId=" + bpdId);
            log.debug("get timezoneOffset=" + timezoneOffset);
        }
        return bs.get(tenantId, bpdId, lang, timezoneOffset);
    }

    @RequestMapping(value="/bpd/save", method= RequestMethod.POST)
    public JSONObject save(HttpServletRequest req, PBpdWithBLOBs bpd) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("save tenantId=" + tenantId);
            log.debug("save loginName=" + loginName);
            log.debug("save displayName=" + displayName);
            log.debug("save bpdId=" + bpd.getBpdId());
            log.debug("save bpdName=" + bpd.getBpdName());
        }
        return bs.save(tenantId, loginName, displayName, bpd);
    }

    @RequestMapping(value="/bpd/saveModel", method= RequestMethod.POST)
    public String saveModel(HttpServletRequest req, String bpd) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("saveModel tenantId=" + tenantId);
            log.debug("saveModel loginName=" + loginName);
            log.debug("saveModel displayName=" + displayName);
            log.debug("saveModel bpd=" + bpd);
        }
        int rtn = bs.saveModel(tenantId, loginName, displayName, bpd);
        return rtn==1 ? Const.AJAX_SUCC : Const.AJAX_FALSE;
    }

    @RequestMapping(value="/bpd/clone", method= RequestMethod.POST)
    public JSONObject clone(HttpServletRequest req, PBpdWithBLOBs bpd) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("clone tenantId=" + tenantId);
            log.debug("clone loginName=" + loginName);
            log.debug("clone displayName=" + displayName);
            log.debug("clone bpdId=" + bpd.getBpdId());
            log.debug("clone bpdName=" + bpd.getBpdName());
        }
        return bs.clone(tenantId, loginName, displayName, bpd);
    }

    @RequestMapping(value="/bpd/{bpdId}", method= RequestMethod.DELETE)
    public JSONObject del(HttpServletRequest req,
                                   @PathVariable("bpdId")String bpdId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("del tenantId=" + tenantId);
            log.debug("del loginName=" + loginName);
            log.debug("del displayName=" + displayName);
            log.debug("del bpdId=" + bpdId);
        }
        return bs.delete(tenantId, loginName, displayName, bpdId);
    }

    @RequestMapping(value="/bpd/deploy/{bpdId}", method= RequestMethod.GET)
    public JSONObject deploy(HttpServletRequest req, @PathVariable("bpdId")String bpdId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("deploy tenantId=" + tenantId);
            log.debug("deploy loginName=" + loginName);
            log.debug("deploy displayName=" + displayName);
            log.debug("deploy bpdId=" + bpdId);
        }
        return bs.deploy(tenantId, loginName, displayName, bpdId);
    }

    @PostMapping(value="/bpd/checkName",
            produces = "plain/text")
    public String checkName(HttpServletRequest req,
                          @RequestParam(name="bpdId",required = false)String bpdId,
                          @RequestParam("bpdName")String bpdName) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkName tenantId=" + tenantId);
            log.debug("checkName bpdId=" + bpdId);
            log.debug("checkName bpdName=" + bpdName);
        }
        return bs.checkName(tenantId, bpdId, bpdName);
    }

    @RequestMapping(value="/bpd/getAuth",
            method= RequestMethod.GET)
    public JSONObject getAuth(HttpServletRequest req,
                              @RequestParam(name="bpdId")String bpdId,
                              @RequestParam("authType")String authType,
                              @RequestParam("memberType")String memberType,
                              @RequestParam("offset")int offset,
                              @RequestParam("limit")int limit) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("getAuth tenantId=" + tenantId);
            log.debug("getAuth bpdId=" + bpdId);
            log.debug("getAuth authType=" + authType);
            log.debug("getAuth memberType=" + memberType);
            log.debug("getAuth offset=" + offset);
            log.debug("getAuth limit=" + limit);
        }
        return bas.getAuth(tenantId, offset, limit, bpdId, null, authType, memberType);
    }

    @RequestMapping(value="/bpd/addAuth",
            method= RequestMethod.POST)
    public JSONObject getAuth(HttpServletRequest req,
                              @RequestParam(name="bpdId")String bpdId,
                              @RequestParam("authType")String authType,
                              @RequestParam("memberType")String memberType,
                              @RequestParam("members")String members) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("addAuth tenantId=" + tenantId);
            log.debug("addAuth bpdId=" + bpdId);
            log.debug("addAuth authType=" + authType);
            log.debug("addAuth memberType=" + memberType);
            log.debug("addAuth members=" + members);
        }
        JSONArray ma = JSONArray.parseArray(members);
        return bas.addAuth(tenantId, loginName, displayName, bpdId, null, authType, memberType, ma);
    }

    @RequestMapping(value="/bpd/delAuth",
            method= RequestMethod.DELETE)
    public JSONObject delAuth(HttpServletRequest req,
                              @RequestParam(name="bpdId")String bpdId,
                              @RequestParam("authType")String authType,
                              @RequestParam("memberType")String memberType,
                              @RequestParam("memberId")String memberId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delAuth tenantId=" + tenantId);
            log.debug("delAuth bpdId=" + bpdId);
            log.debug("delAuth authType=" + authType);
            log.debug("delAuth memberType=" + memberType);
            log.debug("delAuth memberId=" + memberId);
        }
        return bas.delAuth(tenantId, loginName, displayName, bpdId, null, authType, memberType, memberId);
    }

    @RequestMapping(value="/bpd/getVersions", method= RequestMethod.GET)
    public JSONObject getVersions(HttpServletRequest req,
                                  @RequestParam(name="bpdId")String bpdId,
                                  @RequestParam(name="search", required = false)String search,
                                  @RequestParam(name="sort", required = false)String sort,
                                  @RequestParam(name="order", required = false)String order,
                                  @RequestParam("offset")int offset,
                                  @RequestParam("limit")int limit,
                                  @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("getVersions tenantId=" + tenantId);
            log.debug("getVersions bpdId=" + bpdId);
            log.debug("getVersions search=" + search);
            log.debug("getVersions sort=" + sort);
            log.debug("getVersions order=" + order);
            log.debug("getVersions offset=" + offset);
            log.debug("getVersions limit=" + limit);
            log.debug("getVersions timezoneOffset=" + timezoneOffset);
        }
        return bs.getVersions(tenantId, bpdId, search, sort, order, offset, limit, lang, timezoneOffset);
    }
}
